// #include "my_inc/main_login.h"
// #include "my_inc/music.h"

// // 菜单按钮事件
// void menu_music(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     if(!m_btn_p->sign_obj) {
//         lv_obj_add_flag(m_btn_p->play_obj, LV_OBJ_FLAG_HIDDEN); // 隐藏OBJ
//         lv_obj_clear_flag(m_btn_p->obj, LV_OBJ_FLAG_HIDDEN);    // 显示OBJ
//         m_btn_p->sign_obj = 1;
//     } else {
//         lv_obj_add_flag(m_btn_p->obj, LV_OBJ_FLAG_HIDDEN);        // 隐藏OBJ
//         lv_obj_clear_flag(m_btn_p->play_obj, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
//         m_btn_p->sign_obj = 0;
//     }
// }

// // 播放按钮事件
// void menu_play(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     lv_obj_add_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN);    // 隐藏OBJ
//     lv_obj_clear_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
//     if(!m_btn_p->p_sign) {
//         char mp3_name[40];
//         sprintf(mp3_name, "cat /nfs/mp3/%d.mp3 | madplay - &", m_btn_p->mp3);
//         system(mp3_name);
//         m_btn_p->p_sign = 1;
//     } else {
//         system("killall -SIGCONT madplay");
//     }
// }

// // 暂停按钮事件
// void menu_pause(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     lv_obj_add_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);  // 隐藏OBJ
//     lv_obj_clear_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
//     if(m_btn_p->p_sign) {
//         system("killall -SIGSTOP madplay");
//     }
// }

// // 上一首按钮事件
// void menu_prev(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     if(m_btn_p->p_sign) system("killall -SIGTERM madplay");
//     m_btn_p->mp3 = (m_btn_p->mp3 == 1) ? ALL_MUSICS : m_btn_p->mp3 - 1;
//     // 等待一段时间确保进程完全退出
//     usleep(100000); // 100ms延迟
//     char mp3_name[40];
//     sprintf(mp3_name, "cat /nfs/mp3/%d.mp3 | madplay - &", m_btn_p->mp3);
//     system(mp3_name);
//     m_btn_p->p_sign = 1;
// }

// // 下一首按钮事件
// void menu_next(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     if(m_btn_p->p_sign) system("killall -SIGTERM madplay");
//     m_btn_p->mp3 = m_btn_p->mp3 % ALL_MUSICS + 1;
//     // 等待一段时间确保进程完全退出
//     usleep(100000); // 100ms延迟
//     char mp3_name[40];
//     sprintf(mp3_name, "cat /nfs/mp3/%d.mp3 | madplay - &", m_btn_p->mp3);
//     system(mp3_name);
//     m_btn_p->p_sign = 1;
// }

// // home按钮事件
// void menu_home(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     lv_obj_t * scr  = find(m_btn_p->head, "主界面")->screen;
//     lv_scr_load(scr);
// }

// // 退出按钮事件
// void menu_ret(lv_event_t * e)
// {
//     music_p m_btn_p = lv_event_get_user_data(e);
//     lv_obj_t * scr  = find(m_btn_p->head, "主界面")->screen;

//     if(m_btn_p->p_sign) {
//         system("killall -SIGTERM madplay");
//         m_btn_p->p_sign = 0;
//     }
//     lv_obj_add_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);  // 隐藏OBJ
//     lv_obj_clear_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
//     lv_obj_add_flag(m_btn_p->obj, LV_OBJ_FLAG_HIDDEN);        // 隐藏OBJ
//     lv_obj_clear_flag(m_btn_p->play_obj, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
//     lv_scr_load(scr);
// }

#include "my_inc/main_login.h"
#include "my_inc/music.h"
#include <sys/stat.h>

static int music_len[ALL_MUSICS]     = {217, 245, 243, 258, 253};
static char * music_pho[ALL_MUSICS]  = {"A:img/music/c_dzq.png", "A:img/music/dzq2.png", "A:img/music/_3.png",
                                        "A:img/music/dzq2.png", "A:img/music/c_dzq.png"};
static char * music_name[ALL_MUSICS] = {"多远都要在一起", "来自天堂的魔鬼", "暖暖", "泡沫", "唯一"};
int sign_p                           = 0;

// 时间格式转换函数
static void format_time(int seconds, char * buffer)
{
    int mins = seconds / 60;
    int secs = seconds % 60;
    sprintf(buffer, "%02d:%02d", mins, secs);
}

// 获取MP3时长（从madplay输出中解析）
static int get_mp3_duration(const char * filename, music_p m_btn_p)
{
    int duration = 0;
    char cmd[256];
    char result[256];

    // 方法1: 尝试从madplay输出中解析时长
    sprintf(cmd, "madplay \"%s\" -v 2>&1 | grep 'frames decoded'", filename);

    FILE * fp = popen(cmd, "r");
    if(fp) {
        while(fgets(result, sizeof(result), fp)) {
            // 解析格式: "8336 frames decoded (0:03:37.7), ..."
            if(strstr(result, "frames decoded")) {
                int min, sec;
                float sec_frac;
                if(sscanf(result, "%*d frames decoded (%d:%d:%f)", &min, &sec, &sec_frac) >= 2) {
                    duration = min * 60 + sec;
                    printf("解析到时长: %d分%d秒 = %d秒\n", min, sec, duration);
                    break;
                }
            }
        }
        pclose(fp);
    }

    // 方法2: 如果解析失败，使用文件大小估算
    if(duration <= 0) {
        struct stat st;
        if(stat(filename, &st) == 0) {
            duration = (st.st_size * 8) / (128 * 1000);
            printf("估算时长: %d秒 (基于文件大小)\n", duration);
        } else {
            duration = music_len[m_btn_p->mp3 - 1]; // 默认
            printf("使用默认时长: %d秒\n", duration);
        }
    }
    return duration;
}

// 进度更新定时器回调
static void progress_timer_cb(lv_timer_t * timer)
{
    music_p m_btn_p          = timer->user_data;
    progress_state_t * state = &m_btn_p->progress_state;

    if(state->is_playing) {
        // 基于实际播放时间计算进度
        time_t current_time     = time(NULL);
        state->current_position = (int)(current_time - state->start_time);

        // 更新进度条
        if(state->total_duration > 0) {
            int progress = (state->current_position * 100) / state->total_duration;
            progress     = (progress > 100) ? 100 : progress; // 限制不超过100%
            lv_bar_set_value(state->progress_bar, progress, LV_ANIM_ON);
        }

        // 更新时间显示
        char current_time_str[10];
        char total_time_str[10];
        char display_text[20];

        format_time(state->current_position, current_time_str);
        format_time(state->total_duration, total_time_str);
        sprintf(display_text, "%s / %s", current_time_str, total_time_str);

        lv_label_set_text(state->time_label, display_text);

        // 检查是否播放完成
        if(state->current_position >= state->total_duration) {
            state->is_playing = false;
            // 自动播放下一首
            // menu_next(e); // 需要传递事件参数，或者单独实现
        }
    }
}

// 创建进度条UI
void create_progress_ui(music_p m_btn_p)
{
    lv_obj_t * scr = find(m_btn_p->head, "音乐")->screen;
    // 创建进度条
    m_btn_p->progress_state.progress_bar = lv_bar_create(scr);
    lv_obj_set_size(m_btn_p->progress_state.progress_bar, 200, 15);
    lv_obj_align(m_btn_p->progress_state.progress_bar, LV_ALIGN_BOTTOM_LEFT, 50, -90);
    lv_bar_set_range(m_btn_p->progress_state.progress_bar, 0, 100);
    lv_bar_set_value(m_btn_p->progress_state.progress_bar, 0, LV_ANIM_OFF);

    // 设置进度条样式
    lv_obj_set_style_bg_color(m_btn_p->progress_state.progress_bar, lv_color_hex(0x3498db), LV_PART_MAIN);
    lv_obj_set_style_bg_color(m_btn_p->progress_state.progress_bar, lv_color_hex(0x2ecc71), LV_PART_INDICATOR);

    // 创建时间标签
    m_btn_p->progress_state.time_label = lv_label_create(scr);
    lv_obj_align(m_btn_p->progress_state.time_label, LV_ALIGN_BOTTOM_LEFT, 50, -120);
    lv_label_set_text(m_btn_p->progress_state.time_label, "00:00 / 00:00");
    lv_obj_set_style_text_color(m_btn_p->progress_state.time_label, lv_color_white(), 0);
    lv_obj_set_style_text_font(m_btn_p->progress_state.time_label, &lv_font_montserrat_20, 0);
}

// 开始播放时初始化进度状态
static void start_progress_tracking(music_p m_btn_p, int mp3_number)
{
    char filename[40];
    sprintf(filename, "/nfs/mp3/%d.mp3", mp3_number);

    // 获取歌曲时长
    m_btn_p->progress_state.total_duration   = get_mp3_duration(filename, m_btn_p);
    m_btn_p->progress_state.current_position = 0;
    m_btn_p->progress_state.start_time       = time(NULL);
    m_btn_p->progress_state.is_playing       = true;

    // 重置进度条
    lv_bar_set_value(m_btn_p->progress_state.progress_bar, 0, LV_ANIM_OFF);

    // 更新时间显示
    char total_time_str[10];
    char display_text[20];
    format_time(m_btn_p->progress_state.total_duration, total_time_str);
    sprintf(display_text, "00:00 / %s", total_time_str);
    lv_label_set_text(m_btn_p->progress_state.time_label, display_text);

    // 创建或重启定时器
    if(!m_btn_p->progress_timer) {
        m_btn_p->progress_timer = lv_timer_create(progress_timer_cb, 1000, m_btn_p);
    } else {
        lv_timer_reset(m_btn_p->progress_timer);
    }
}

// 停止进度跟踪
static void stop_progress_tracking(music_p m_btn_p)
{
    m_btn_p->progress_state.is_playing = false; // 1. 设置播放状态为"停止"
    if(m_btn_p->progress_timer) {
        lv_timer_pause(m_btn_p->progress_timer); // 2. 暂停进度更新定时器
    }
}

// 菜单按钮事件
void menu_music(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    if(!m_btn_p->sign_obj) {
        lv_obj_add_flag(m_btn_p->play_obj, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(m_btn_p->obj, LV_OBJ_FLAG_HIDDEN);

        m_btn_p->sign_obj = 1;
    } else {
        lv_obj_add_flag(m_btn_p->obj, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(m_btn_p->play_obj, LV_OBJ_FLAG_HIDDEN);
        m_btn_p->sign_obj = 0;
    }
}

// 播放按钮事件
void menu_play(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    lv_obj_add_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);

    if(!m_btn_p->p_sign) {
        char mp3_name[40];
        sprintf(mp3_name, "cat /nfs/mp3/%d.mp3 | madplay - &", m_btn_p->mp3);
        system(mp3_name);

        // lv_img_set_src(m_btn_p->rotate_state.music_img, NULL);
        // create_rotating_image(m_btn_p,"A:img/music.png");
        m_btn_p->p_sign = 1;
    } else {
        system("killall -SIGCONT madplay");
    }
    if(!sign_p || sign_p == 2) {
        // 设置需要重新初始化的标志
        m_btn_p->need_reinit = true;

        // 检查是否需要重新初始化
        if(m_btn_p->need_reinit) {
            // 重新创建进度条（如果被删除了）
            if(!m_btn_p->progress_state.progress_bar) {
                create_progress_ui(m_btn_p);
            }
            // 重置进度条状态
            lv_bar_set_value(m_btn_p->progress_state.progress_bar, 0, LV_ANIM_OFF);
            lv_label_set_text(m_btn_p->progress_state.time_label, "00:00 / 00:00");
            // 开始进度跟踪
            start_progress_tracking(m_btn_p, m_btn_p->mp3);

            if(m_btn_p->circle_obj) {
                // 如果图片不存在，则创建
                create_rotating_image(m_btn_p, music_pho[m_btn_p->mp3 - 1]);
            }
            // 如果正在播放，继续旋转
            start_music_rotation(m_btn_p);
            m_btn_p->need_reinit = false;
        }
        sign_p = 0;
    }

    // 如果正在播放，继续旋转
    start_music_rotation(m_btn_p);
    // 恢复进度跟踪
    m_btn_p->progress_state.start_time = time(NULL) - m_btn_p->progress_state.current_position;
    m_btn_p->progress_state.is_playing = true;
    if(m_btn_p->progress_timer) {
        lv_timer_resume(m_btn_p->progress_timer);
    }
    printf("%s\n", music_name[m_btn_p->mp3 - 1]);
}

// 暂停按钮事件
void menu_pause(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    lv_obj_add_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN);

    if(m_btn_p->p_sign) {
        system("killall -SIGSTOP madplay");
        // 暂停进度跟踪
        stop_progress_tracking(m_btn_p);
        // 暂停图片旋转
        pause_music_rotation(m_btn_p);
    }
    sign_p = 1;
}

// 上一首按钮事件
void menu_prev(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    lv_obj_add_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);
    if(m_btn_p->p_sign) system("killall -SIGTERM madplay");

    m_btn_p->mp3 = (m_btn_p->mp3 == 1) ? ALL_MUSICS : m_btn_p->mp3 - 1;

    // 等待一段时间确保进程完全退出
    usleep(100000);

    char mp3_name[40];
    sprintf(mp3_name, "cat /nfs/mp3/%d.mp3 | madplay - &", m_btn_p->mp3);
    system(mp3_name);
    m_btn_p->p_sign = 1;

    // 重置旋转角度为0
    m_btn_p->rotate_state.current_angle = 0;
    if(m_btn_p->rotate_state.music_img) {
        lv_img_set_angle(m_btn_p->rotate_state.music_img, 0);
    }

    printf("%s\n", music_name[m_btn_p->mp3 - 1]);
    // 直接更新现有图片的源，而不是重新创建
    if(m_btn_p->rotate_state.music_img) {
        lv_img_set_src(m_btn_p->rotate_state.music_img, music_pho[m_btn_p->mp3 - 1]);
        lv_img_set_angle(m_btn_p->rotate_state.music_img, 0);
    } else if(m_btn_p->circle_obj) {
        // 如果图片不存在，则创建
        create_rotating_image(m_btn_p, music_pho[m_btn_p->mp3 - 1]);
    }

    lv_label_set_text(m_btn_p->label_w, music_name[m_btn_p->mp3 - 1]);
    // 设置需要重新初始化的标志
    m_btn_p->need_reinit = true;

    // 检查是否需要重新初始化
    if(m_btn_p->need_reinit) {

        // 重新创建进度条（如果被删除了）
        if(!m_btn_p->progress_state.progress_bar) {
            create_progress_ui(m_btn_p);
        }

        // 重置进度条状态
        lv_bar_set_value(m_btn_p->progress_state.progress_bar, 0, LV_ANIM_OFF);
        lv_label_set_text(m_btn_p->progress_state.time_label, "00:00 / 00:00");
        // 开始进度跟踪
        start_progress_tracking(m_btn_p, m_btn_p->mp3);

        m_btn_p->need_reinit = false;
    }
    if(sign_p == 2) {
 // 恢复进度跟踪
    m_btn_p->progress_state.start_time = time(NULL) - m_btn_p->progress_state.current_position;
    m_btn_p->progress_state.is_playing = true;
    if(m_btn_p->progress_timer) {
        lv_timer_resume(m_btn_p->progress_timer);
    }
    }

    // 如果正在播放，继续旋转
    start_music_rotation(m_btn_p);
}

// 下一首按钮事件
void menu_next(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    lv_obj_add_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);
    if(m_btn_p->p_sign) system("killall -SIGTERM madplay");

    m_btn_p->mp3 = m_btn_p->mp3 % ALL_MUSICS + 1;

    // 等待一段时间确保进程完全退出
    usleep(100000);

    char mp3_name[40];
    sprintf(mp3_name, "cat /nfs/mp3/%d.mp3 | madplay - &", m_btn_p->mp3);
    system(mp3_name);
    m_btn_p->p_sign = 1;

    // 重置旋转角度为0
    m_btn_p->rotate_state.current_angle = 0;
    if(m_btn_p->rotate_state.music_img) {
        lv_img_set_angle(m_btn_p->rotate_state.music_img, 0);
    }

    printf("%s\n", music_name[m_btn_p->mp3 - 1]);
    // 直接更新现有图片的源，而不是重新创建
    if(m_btn_p->rotate_state.music_img) {
        lv_img_set_src(m_btn_p->rotate_state.music_img, music_pho[m_btn_p->mp3 - 1]);
        lv_img_set_angle(m_btn_p->rotate_state.music_img, 0);
    } else if(m_btn_p->circle_obj) {
        // 如果图片不存在，则创建
        create_rotating_image(m_btn_p, music_pho[m_btn_p->mp3 - 1]);
    }

    lv_label_set_text(m_btn_p->label_w, music_name[m_btn_p->mp3 - 1]);
    // 设置需要重新初始化的标志
    m_btn_p->need_reinit = true;

    // 检查是否需要重新初始化
    if(m_btn_p->need_reinit) {
        // 重新创建进度条（如果被删除了）
        if(!m_btn_p->progress_state.progress_bar) {
            create_progress_ui(m_btn_p);
        }

        // 重置进度条状态
        lv_bar_set_value(m_btn_p->progress_state.progress_bar, 0, LV_ANIM_OFF);
        lv_label_set_text(m_btn_p->progress_state.time_label, "00:00 / 00:00");

        // 开始进度跟踪
        start_progress_tracking(m_btn_p, m_btn_p->mp3);

        m_btn_p->need_reinit = false;
    }
        if(sign_p == 2) {
 // 恢复进度跟踪
    m_btn_p->progress_state.start_time = time(NULL) - m_btn_p->progress_state.current_position;
    m_btn_p->progress_state.is_playing = true;
    if(m_btn_p->progress_timer) {
        lv_timer_resume(m_btn_p->progress_timer);
    }
    }
    // 如果正在播放，继续旋转
    start_music_rotation(m_btn_p);
}

// home按钮事件
void menu_home(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    lv_obj_t * scr  = find(m_btn_p->head, "主界面")->screen;

    sign_p = 1;
    lv_scr_load(scr);
}

// 退出按钮事件
void menu_ret(lv_event_t * e)
{
    music_p m_btn_p = lv_event_get_user_data(e);
    lv_obj_t * scr  = find(m_btn_p->head, "主界面")->screen;

    // 如果音乐正在播放，停止它
    if(m_btn_p->p_sign) {
        // system("killall -SIGTERM madplay");
        // system("pkill madplay");//终止所有madplay进程
        system("pkill -9 madplay"); // 发送SIGKILL信号，强制终止所有名为 madplay 的进程
        // 停止进度跟踪
        stop_progress_tracking(m_btn_p);
        m_btn_p->p_sign = 0;
    }

    m_btn_p->rotate_state.is_rotating = 0;
    cleanup_music_rotation(m_btn_p);

    lv_obj_add_flag(m_btn_p->btn_pause, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(m_btn_p->btn_play, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(m_btn_p->obj, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(m_btn_p->play_obj, LV_OBJ_FLAG_HIDDEN);

    // 设置需要重新初始化的标志
    m_btn_p->need_reinit = true;

    // 检查是否需要重新初始化
    if(m_btn_p->need_reinit) {
        // 重新创建旋转图片
        if(m_btn_p->circle_obj && !m_btn_p->rotate_state.music_img) {
            create_rotating_image(m_btn_p, "A:img/c_dzq.png");
        }

        // 重新创建进度条（如果被删除了）
        if(!m_btn_p->progress_state.progress_bar) {
            create_progress_ui(m_btn_p);
        }

        // 重置进度条状态
        lv_bar_set_value(m_btn_p->progress_state.progress_bar, 0, LV_ANIM_OFF);
        lv_label_set_text(m_btn_p->progress_state.time_label, "00:00 / 00:00");

        m_btn_p->need_reinit = false;
    }

    sign_p = 2;
    lv_scr_load(scr);
}

// 旋转定时器回调函数
void rotate_timer_cb(lv_timer_t * timer)
{
    music_rotate_t * rotate_state = (music_rotate_t *)timer->user_data;
    if(!rotate_state || !rotate_state->is_rotating) return;

    // 更新旋转角度
    rotate_state->current_angle += 10; // 每次增加10度
    if(rotate_state->current_angle >= 3600) {
        rotate_state->current_angle = 0;
    }

    // 应用旋转
    if(rotate_state->music_img) {
        lv_img_set_angle(rotate_state->music_img, rotate_state->current_angle);
    }
}

// 创建旋转图片对象
void create_rotating_image(music_p m_btn_p, const void * img_src)
{
    if(!m_btn_p) return;

    // 创建图片对象
    m_btn_p->rotate_state.music_img = lv_img_create(m_btn_p->circle_obj);
    lv_img_set_src(m_btn_p->rotate_state.music_img, img_src);
    lv_obj_center(m_btn_p->rotate_state.music_img);
    // lv_obj_set_size(m_btn_p->rotate_state.music_img, 160, 160); // 设置图片大小

    // 设置旋转中心点（图片中心）
    lv_img_set_pivot(m_btn_p->rotate_state.music_img, 180, 180);

    // 初始化旋转状态
    m_btn_p->rotate_state.current_angle = 0;    // 设置当前角度为0度
    m_btn_p->rotate_state.is_rotating   = 0;    // 设置旋转状态为"停止"
    m_btn_p->rotate_state.rotate_timer  = NULL; // 定时器指针初始化为NULL

    // 创建旋转定时器（初始暂停状态）
    m_btn_p->rotate_state.rotate_timer = lv_timer_create(rotate_timer_cb, 50, &m_btn_p->rotate_state);
    lv_timer_pause(m_btn_p->rotate_state.rotate_timer); // 创建后立即暂停
}

// 开始旋转
void start_music_rotation(music_p m_btn_p)
{
    if(!m_btn_p || !m_btn_p->rotate_state.rotate_timer) return;

    m_btn_p->rotate_state.is_rotating = 1;
    lv_timer_resume(m_btn_p->rotate_state.rotate_timer);
}

// 暂停旋转
void pause_music_rotation(music_p m_btn_p)
{
    if(!m_btn_p || !m_btn_p->rotate_state.rotate_timer) return;

    m_btn_p->rotate_state.is_rotating = 0;
    lv_timer_pause(m_btn_p->rotate_state.rotate_timer);
}

// 停止旋转（重置角度）
void stop_music_rotation(music_p m_btn_p)
{
    if(!m_btn_p) return;

    m_btn_p->rotate_state.is_rotating   = 0;
    m_btn_p->rotate_state.current_angle = 0;

    if(m_btn_p->rotate_state.music_img) {
        lv_img_set_angle(m_btn_p->rotate_state.music_img, 0);
    }

    if(m_btn_p->rotate_state.rotate_timer) {
        lv_timer_pause(m_btn_p->rotate_state.rotate_timer);
    }
}

// 更改旋转图片
void change_rotation_image(music_p m_btn_p, const void * new_img_src)
{
    if(!m_btn_p || !m_btn_p->rotate_state.music_img) return;

    // 暂停旋转
    int was_rotating = m_btn_p->rotate_state.is_rotating;
    if(was_rotating) {
        pause_music_rotation(m_btn_p);
    }

    // 更新图片
    lv_img_set_src(m_btn_p->rotate_state.music_img, new_img_src);

    // 重置角度
    m_btn_p->rotate_state.current_angle = 0;
    lv_img_set_angle(m_btn_p->rotate_state.music_img, 0);

    // 如果之前在旋转，恢复旋转
    if(was_rotating) {
        start_music_rotation(m_btn_p);
    }
}

// 设置旋转速度（毫秒）
void set_rotation_speed(music_p m_btn_p, uint32_t period_ms)
{
    if(!m_btn_p || !m_btn_p->rotate_state.rotate_timer) return;

    lv_timer_set_period(m_btn_p->rotate_state.rotate_timer, period_ms);
}

// 清理旋转资源
void cleanup_music_rotation(music_p m_btn_p)
{
    if(!m_btn_p) return;

    if(m_btn_p->rotate_state.rotate_timer) {
        lv_timer_del(m_btn_p->rotate_state.rotate_timer);
        m_btn_p->rotate_state.rotate_timer = NULL;
    }

    // 重置所有旋转状态
    m_btn_p->rotate_state.is_rotating   = 0;
    m_btn_p->rotate_state.current_angle = 0;

    // 注意：不要删除circle_obj和music_img，只重置指针
    m_btn_p->rotate_state.circle_obj = NULL;
    m_btn_p->rotate_state.music_img  = NULL;
}
